﻿CREATE PROCEDURE [dbo].[proc_CheckOut_GetEmployeeList_mobilecheck]
(
	@StartIndex int,
	@EndIndex INT,
	@UserName nvarchar(50),
	@CompanyId varchar(50),
	@DepartmentId varchar(50),
	@StartDate varchar(50),
	@EndDate varchar(50)
)
AS
Begin
declare @strSql NVARCHAR(4000)
set @strSql=''
if @CompanyId<>'0'
begin
	set @strSql=@strSql+' CompanyId='''+@CompanyId+''' and '
end
if @DepartmentId<>'0'
begin
   set @strSql=@strSql+' DepartmentId='''+@DepartmentId+''' and '
end
if @UserName<>''
begin
   Set @strSql=@strSql+' UserName='''+Convert(varchar,@UserName)+''' and '
end

set @strSql='

	Declare @sRq Datetime,@eRq Datetime
	Set @sRq = Convert(Datetime,'''+@StartDate+' 00:00:00'')
	Set @eRq = Convert(Datetime,'''+@EndDate+' 23:59:59'')
	;WITH List As(Select ROW_NUMBER() OVER (Order By CheckOutTime Desc) AS Row,
		Id,
		CompanyId,
		CustomerId,
		(select CustomerName from Customer where CheckInOut.CustomerId = Customer.CustomerId) as CustomerName,
		ContactPerson,
		ContactTel, 
		Address,
		MainProjectId,
		(Select Title From Product Where Id=CheckInOut.MainProjectId) As MainProject,
		Content,
		UserName,
		CheckOutTime,
		ISNULL(CheckInTime,'''')AS CheckInTime,

		Latitude,
		Longitude,
		PositionTime,
		PositionAddress,
		(Select EName from Employee where UserName=CheckInOut.UserName) as EName --签到人姓名
		From CheckInOut
	           	Where '+@strSql+'  PositionTime>=@sRq And PositionTime<=@eRq  
	)
	Select *,(Select Count(0) From list) As RecordCount From list Where Row  Between '+Convert(varchar,@StartIndex)+' and '+Convert(varchar,@EndIndex)+' Order By Row'
	--print(@strSql)
	exec(@strSql)
	--return @strSql

End